package src;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.JavascriptExecutor;

public class LoginTest {
    private WebDriver driver;

    public LoginTest(WebDriver driver) {
        this.driver = driver;
    }

    public void testLogin() throws InterruptedException {
        // 根据网址打开首页
        driver.get("https://www.bilibili.com");
    
        // 创建JavaScript执行器，用于执行页面滚动
        JavascriptExecutor js = (JavascriptExecutor) driver;
    
        // 滚动页面到底部，让所有推荐内容都被加载
        js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
        Thread.sleep(2000);
    
        // 回到页面顶部，测试登录按钮是否加载正常
        js.executeScript("window.scrollTo(0, 0);");
    
        // 点击登录按钮
        WebElement loginButton = driver.findElement(By.cssSelector(".header-login-entry"));
        if (!loginButton.isDisplayed()) {
            throw new RuntimeException("Error: 登录按钮未加载！");
        }

        // 查找按钮内部的<span>并验证其内容是否为"登录"
        WebElement loginButtonText = loginButton.findElement(By.tagName("span"));
        if (!loginButtonText.getText().contains("登录")) {
            throw new RuntimeException("Error: 登录按钮的文本内容不是'登录'！");
        }
        loginButton.click();
    
        // 测试登录功能
        // 获取账号输入框并输入账号
        WebElement usernameField = driver.findElement(By.cssSelector(".tab__form > :nth-child(1) > input"));
        if (!usernameField.isDisplayed()) {
            throw new RuntimeException("Error: 账号输入框未加载！");
        }
        usernameField.sendKeys("123456789");
    
        // 获取密码输入框并输入密码
        WebElement passwordField = driver.findElement(By.cssSelector(":nth-child(3) > input"));
        if (!passwordField.isDisplayed()) {
            throw new RuntimeException("Error: 密码输入框未加载！");
        }
        passwordField.sendKeys("123456789");
    
        // 检查登录按钮是否正常显示并点击
        WebElement submitButton = driver.findElement(By.cssSelector(".btn_primary"));
        if (!submitButton.isDisplayed()) {
            throw new RuntimeException("Error: 登录按钮未加载！");
        }
        Thread.sleep(2000);
        submitButton.click();
        Thread.sleep(2000);
    
        // 测试人机验证图片的刷新
        WebElement refreshCaptcha = driver.findElement(By.cssSelector(".geetest_refresh"));
        if (!refreshCaptcha.isDisplayed()) {
            throw new RuntimeException("Error: 验证码刷新按钮未加载！");
        }
        Thread.sleep(2000);
        refreshCaptcha.click();
        Thread.sleep(2000);
        refreshCaptcha.click();
    
        // 刷新后确保图片加载稳定后再点击
        Thread.sleep(4000);
        WebElement captchaImage = driver.findElement(By.cssSelector(".geetest_item_img"));
        if (!captchaImage.isDisplayed()) {
            throw new RuntimeException("Error: 验证码图片未加载！");
        }
        Thread.sleep(2000);
        captchaImage.click();
        Thread.sleep(2000);
        
        // 点击确认提交人机验证结果
        WebElement commitCaptchaButton = driver.findElement(By.cssSelector(".geetest_commit_tip"));
        if (!commitCaptchaButton.isDisplayed()) {
            throw new RuntimeException("Error: 提交人机验证结果按钮未加载！");
        }
        Thread.sleep(2000); // 等待确保按钮完全加载
        commitCaptchaButton.click();
        Thread.sleep(500); 

        // 点击确认提交人机验证结果并断言“验证失败”提示是否存在
        WebElement failureMessage = driver.findElement(By.cssSelector(".geetest_result_tip"));
        if (failureMessage.isDisplayed() && failureMessage.getText().contains("验证失败 请按提示重新操作")) {
            System.out.println("CAPTCHA validation failed as expected.");
        } else {
            throw new RuntimeException("Error: 验证失败提示未出现！");
        }
    
        // 点击关闭人机验证窗口
        WebElement closeCaptchaButton = driver.findElement(By.cssSelector(".geetest_close"));
        if (!closeCaptchaButton.isDisplayed()) {
            throw new RuntimeException("Error: 验证码关闭按钮未加载！");
        }
        Thread.sleep(2000);
        closeCaptchaButton.click();
    
        // 点击关闭登录窗口
        WebElement closeLoginWindow = driver.findElement(By.cssSelector(".bili-mini-close-icon"));
        if (!closeLoginWindow.isDisplayed()) {
            throw new RuntimeException("Error: 登录窗口关闭按钮未加载！");
        }
        Thread.sleep(2000);
        closeLoginWindow.click();
    }
}
